#include "stdafx.h"
#include "GameServer.h"

CGameServer::CGameServer()
	:m_isc(&m_settings.ISC, this)
{
}

CGameServer::~CGameServer()
{
	m_net.shutdown();
}

void CGameServer::start()
{
	if(!_load())
		return;

	while(_main());
}

bool CGameServer::_load()
{
	_setTitle();

	CConsole::initCloseHandler();

	CConsole::print("\n                        ===============================\n");
	CConsole::print("                                  DevOnServer\n");
	CConsole::print("                                GameServer v%u.%u\n", Version, SubVersion);
	CConsole::print("                        ===============================\n\n");

	CConsole::print(" Loading...\n");

	if(!m_settings.load("GameServer.ini"))
	{
		CConsole::print(" Can't load configuration file 'GameServer.ini'.\n");
		return false;
	}

	if(!m_net.startup())
	{
		CConsole::print(" Network can't be started.\n");
		return false;
	}

	m_net.setWaitingTime(33);

	if(!m_isc.init(&m_net))
	{
		CConsole::print(" ISC client can't be initialized.\n");
		return false;
	}

	CConsole::print(" Server is running !\n\n");

	return true;
}

bool CGameServer::_main()
{
	if(CConsole::isClosed())
		return false;

	return m_net.run()
		&& m_isc.run();
}

void CGameServer::_setTitle()
{
	char buffer[256];
	sprintf(buffer, "GameServer v%u.%u", Version, SubVersion);
	CConsole::setTitle(buffer);
}